package org.springdemo.user.controller;

import org.springdemo.redis.RedisService;
import org.springdemo.user.service.UserFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.springdemo.vo.UserVo;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserFeignService userFeignService;
	@Autowired
	RedisService redisService;
	
	
	@GetMapping("/findUserById/{userId}")
	public UserVo findUserById(@PathVariable String userId){
		
		UserVo user = null;
		if(redisService.hasKey(userId)){
			user = new Gson().fromJson(redisService.get(userId).toString(), UserVo.class);
		}else{
			user = userFeignService.findUserById(userId);
			redisService.set(userId, new Gson().toJson(user));
		}
		return user;
	}
	
	
}
